package com.hfdg.authority.model;

import com.hfdg.authority.entity.AuthMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:TreeData
 * @Description: TODO
 * @Author: yuelh
 * @Date: 2019/3/30 21:28
 * Version: V1.0
 */
public class AuthMenuTreeNode {
    String id;
    String parentId;
    String text;
    String url;
    String icon;
    String targetType;
    List<AuthMenuTreeNode> children;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getParentId() {
        return parentId;
    }
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
    public String getTargetType() {
        return targetType;
    }
    public void setTargetType(String targetType) {
        this.targetType = targetType;
    }
    public List<AuthMenuTreeNode> getChildren() {
        return children;
    }
    public void setChildren(List<AuthMenuTreeNode> children) {
        this.children = children;
    }
    public static List<AuthMenuTreeNode> buildTreeData(List<AuthMenu> authMenuList){
        List<AuthMenuTreeNode> treeNodeList = new ArrayList<>();
        for(AuthMenu authMenu:authMenuList){
            AuthMenuTreeNode treeNode = new AuthMenuTreeNode();
            treeNode.setParentId(authMenu.getParentUid());
            treeNode.setId(authMenu.getUid());
            treeNode.setText(authMenu.getName());
            treeNode.setUrl(authMenu.getHref());
            treeNode.setIcon(authMenu.getIcon());
            treeNode.setTargetType("iframe-tab");
            treeNodeList.add(treeNode);
        }
     return buildTree(treeNodeList);
    }
    private static List<AuthMenuTreeNode>buildTree(List<AuthMenuTreeNode> list) {
        List<AuthMenuTreeNode> treeList = new ArrayList<AuthMenuTreeNode>();
        for (AuthMenuTreeNode tree : list) {
            //找到根
            if ("0".equals(tree.getParentId())) {
                treeList.add(tree);
            }
            for (AuthMenuTreeNode treeNode : list) {
                if (treeNode.getParentId().trim().equals(tree.getId().trim())) {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<AuthMenuTreeNode>());
                    }
                    tree.getChildren().add(treeNode);
                }
            }
        }
        return treeList;
    }
}